﻿#include <Windows.h>
#include <tchar.h>
#include "Protocol.h"

// 寻找其它用户请求
/**
 * 创建协议。
 * @param type      类型
 * @param username  用户名
 */
GomokuProtocol* New_GomokuProtocol(ProtocolType type, LPCTSTR username) {
    GomokuProtocol* pro = (GomokuProtocol*)calloc(1, sizeof(GomokuProtocol));
    pro->mType = type;
    if (username != NULL) {
        _tcscpy_s(pro->mUsername, USERNAME_BUF_LEN, username);
    }
    return pro;
}

// 寻找其它用户结果
/**
 * 创建寻找其它用户结果。
 */
FindUsersResult* New_FindUsersResult() {
    FindUsersResult* result = (FindUsersResult*)calloc(1, sizeof(FindUsersResult));
    result->mType = FIND_USERS;
    return result;
}

// 游戏结束
/**
 * 创建游戏结束。
 * @param reason    原因
 * @param rivalName 对手的名字
 */
GameOver* New_GameOver(GameOverReason reason, LPCTSTR rivalName) {
    GameOver* gameOver = (GameOver*)calloc(1, sizeof(GameOver));
    gameOver->mType = GAME_OVER;
    gameOver->mReason = reason;
    _tcscpy_s(gameOver->mRivalName, USERNAME_BUF_LEN, rivalName);
    return gameOver;
}

// 挑战请求
/**
 * 创建挑战请求。
 * @param rivalName 对手的名字
 * @param username  用户名
 */
InviteRequest* New_InviteRequest(LPCTSTR rivalName, LPCTSTR username) {
    InviteRequest* reqeust = (InviteRequest*)calloc(1, sizeof(InviteRequest));
    reqeust->mType = INVITE_REQUEST;
    _tcscpy_s(reqeust->mUsername, USERNAME_BUF_LEN, username);
    _tcscpy_s(reqeust->mRivalName, USERNAME_BUF_LEN, rivalName);
    return reqeust;
}

// 挑战结果
/**
 * 创建挑战结果。
 * @param accept    接受挑战则为 TRUE，否则为 FALSE
 * @param username  用户名
 * @param rivalName 对手的名字
 */
InviteResult* New_InviteResult(BOOL accept, LPCTSTR username, LPCTSTR rivalName) {
    InviteResult* result = (InviteResult*)calloc(1, sizeof(InviteResult));
    result->mType = INVITE_RESULT;
    _tcscpy_s(result->mUsername, USERNAME_BUF_LEN, username);
    result->mAccept = accept;
    _tcscpy_s(result->mRivalName, USERNAME_BUF_LEN, rivalName);
    return result;
}

// 放置棋子
/**
 * 创建放置棋子。
 * @param rivalName 对手的名字
 * @param x, y      棋子位置（窗口坐标）
 */
PutPiece* New_PutPiece(LPCTSTR rivalName, int x, int y) {
    PutPiece* putPiece = (PutPiece*)calloc(1, sizeof(PutPiece));
    putPiece->mType = PUT_PIECE;
    _tcscpy_s(putPiece->mRivalName, USERNAME_BUF_LEN, rivalName);
    putPiece->mX = x;
    putPiece->mY = y;
    return putPiece;
}
